package com.geekcoding.geekoj.config;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author xiaobai
 * @since 2024/02/25 12:15
 **/

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Resource
    private WebHandleInterceptor webHandleInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")    // 允许跨域访问的路径
                .allowedOriginPatterns("*")    // 允许跨域访问的源
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")    // 允许请求方法
                .maxAge(168000)    // 预检间隔时间
                .allowedHeaders("*")  // 允许头部设置
                .allowCredentials(true);    // 是否发送cookie
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/").setViewName("index");
//        registry.addViewController("/index").setViewName("index");
//        registry.addViewController("/get-wx-code").setViewName("get-wx-code");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//        //将所有/static/** 访问都映射到classpath:/static/ 目录下
//        registry.addResourceHandler("/static/**", "/resources/**")
//                .addResourceLocations(String.format("%s/static/", ResourceUtils.CLASSPATH_URL_PREFIX));
//
//
//        registry.addResourceHandler("/wx/**", "MP_verify_gGgOnz0Kz7mO1XfO.txt", "1Lh1OkG55U.txt")
//                .addResourceLocations(String.format("%s/static/wx/", ResourceUtils.CLASSPATH_URL_PREFIX));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(webHandleInterceptor)
                .addPathPatterns("/api/**");
    }
}
